/*
  ID: nemokin1
  PROG: inflate
  LANG: C++
*/
#include <vector>
#include <list>
#include <assert.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <fstream>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
 
using namespace std;
 
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=b;--i)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define ALL(c) (c).begin(),(c).end()
 
#define debug(x) cerr<< #x << " = " << (x) << "\n";
#define debugv(v) cerr << #v << " = "; FOREACH(it,v) cerr << *it <<","; cerr << "\n";
#define SIZE(a)  (int(a.size()))
#define mp make_pair
#define pb push_back
 
template<typename T> inline void checkMax(T& a, T b) {if (b>a) a=b;}
template<typename T> inline void checkMin(T& a, T b) {if (b<a) a=b;}
 
typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int>  ii;
typedef long double ld;
const int INF = 1000000000;

int main(){
    freopen("inflate.in","r",stdin);freopen("inflate.out","w",stdout);
    int Max;
    int N;
    cin >> Max >> N;
    vector<int> w;
    vector<int> c;
    
    REP(i,N){
        int m,n;
        cin >> m >> n;
        w.push_back(m);
        c.push_back(n);
    }

    int f[100001];
    REP(i,100001) f[i]=0;
    
    for(int i = 0; i < N; i++){
        for(int v = 0; v <= Max; v++){
            if(v-c[i] < 0) continue;
            f[v] = max(f[v],f[v-c[i]]+w[i]);
        }
    }
    cout << f[Max] << endl;
    
    return 0;
}

		
    

    

